import axios from 'axios';

const serviceAxios = axios.create({
  baseURL: '/abc', // 基础请求地址
  timeout: 100000, // 请求超时设置
});

// 请求拦截
serviceAxios.interceptors.request.use(
  (config) => {
    if(config.method === 'post') {
      config.headers['content-type'] = 'application/x-www-form-urlencoded';
    } else {
      config.headers["content-type"] = "application/json";
    }
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

// 响应拦截
serviceAxios.interceptors.response.use(
  (response) => {
    console.log('🚀 ~ response:', response);
    if (response.status === 500) {
      alert('服务器错误,qing');
    }
    if (response.data.code !== 0) {
      alert(response.data.msg);
      return Promise.reject(response.data);
    }
    return response.data;
  }
);

export default serviceAxios;